<?php
class ControllerModuleWelcomepopup extends Controller {
	private $error = array(); 
	
	public function index() {   
		$this->load->language('module/welcomepopup');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('setting/setting');
		
		$this->load->model('catalog/syscontent');
		
		$this->data['syscontents'] = array();

		foreach ($this->model_catalog_syscontent->getSyscontents() as $result) {
      		$this->data['syscontents'][] = array(
        		'title' => $result['title'],
	    		'syscontent_id'  => $result['syscontent_id'])
      		;
    	}
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			
			$popup_settings = array(
			'welcomepopup_status'    => $this->request->post['popup_status'],
			'welcomepopup_position'  => "home",
			'welcomepopup_sort_order'  => "0",
			'welcomepopup_mode'      => $this->request->post['popup_mode'],
			'welcomepopup_cookie'    => $this->request->post['popup_cookie'],
			'welcomepopup_page'    	 => $this->request->post['popup_page'],
			);	
			
			
			$this->model_setting_setting->editSetting('welcomepopup', $popup_settings);						
			$this->session->data['success'] = $this->language->get('text_success');						
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
				
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_left'] = $this->language->get('text_left');
		$this->data['text_right'] = $this->language->get('text_right');
		$this->data['text_home'] = $this->language->get('text_home');
		
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_mode'] = $this->language->get('entry_mode');
		$this->data['entry_cookie'] = $this->language->get('entry_cookie');
		$this->data['entry_page'] = $this->language->get('entry_page');

		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_module'),
      		'separator' => ' :: '
   		);
		
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=module/welcomepopup&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = HTTPS_SERVER . 'index.php?route=module/welcomepopup&token=' . $this->session->data['token'];
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token'];

		$this->data['token'] = $this->session->data['token'];
		
		
		if (isset($this->request->post['popup_status'])) {
			$this->data['popup_status'] = $this->request->post['popup_status'];
		} else {
			$this->data['popup_status'] = $this->config->get('welcomepopup_status');
		}
		
		
		$this->data['mode'] = array();
		
		$this->data['mode'][] = array(
			'mode' => 'everytimeenter',
			'title'    => $this->language->get('text_mode_everytimeenter'),
		);
		
		$this->data['mode'][] = array(
			'mode' => 'cookie',
			'title'    => $this->language->get('text_mode_cookie'),
		);
		
		
		
		if (isset($this->request->post['popup_mode'])) {
			$this->data['popup_mode'] = $this->request->post['popup_mode'];
		} else {
			$this->data['popup_mode'] = $this->config->get('welcomepopup_mode');
		}
		
		if (isset($this->request->post['popup_cookie'])) {
			$this->data['popup_cookie'] = $this->request->post['popup_cookie'];
		} else {
			$this->data['popup_cookie'] = $this->config->get('welcomepopup_cookie');
		}
		
		if (isset($this->request->post['popup_page'])) {
			$this->data['popup_page'] = $this->request->post['popup_page'];
		} else {
			$this->data['popup_page'] = $this->config->get('welcomepopup_page');
		}	
		
			
		$this->template = 'module/welcomepopup.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/welcomepopup')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
}	
?>